package com.iknow.activity;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.app.IKnowSystemConfig;
import com.iknow.data.QingBo;
import com.iknow.task.CommonTask.QingboTask;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.ui.model.QingBoAdapter;
import com.iknow.util.StringUtil;
import com.iknow.view.widget.PullToRefreshListView;
import com.iknow.view.widget.PullToRefreshListView.OnRefreshListener;
import com.mobclick.android.MobclickAgent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public abstract class QingboWithNoHeaderActivity extends Activity
{
  private AbsListView.OnScrollListener ListViewOnScrollListener;
  private PullToRefreshListView.OnRefreshListener ListViewRefreshListener;
  protected final int MAX_QINGBO_STEP = 20;
  protected int PAGE_TYPE;
  protected String TAG;
  private AdapterView.OnItemClickListener listItemClickListener;
  protected QingBoAdapter mAdapter;
  protected String mAuthorID;
  protected SimpleDateFormat mDateFormat;
  protected Button mGetMoreButton;
  protected TaskListener mGetMoreListener;
  protected PullToRefreshListView mListView;
  protected View mListViewFooter;
  protected LocalDataTask mLocalDataTask;
  protected int mLocalOffset = 0;
  protected CommonTask.QingboTask mTask;
  protected TaskListener mTaskListener;
  public int view = 0;

  public QingboWithNoHeaderActivity()
  {
    this.mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    this.listItemClickListener = new AdapterView.OnItemClickListener(this)
    {
      public void onItemClick(, View paramView, int paramInt, long paramLong)
      {
        QingBo localQingBo = this.this$0.mAdapter.getItem(paramInt - 1);
        if (localQingBo == null);
        while (true)
        {
          return;
          Intent localIntent = new Intent(this.this$0, PageActivity.class);
          localIntent.putExtra("item", localQingBo);
          this.this$0.startActivity(localIntent);
        }
      }
    };
    this.ListViewRefreshListener = new PullToRefreshListView.OnRefreshListener(this)
    {
      public void onRefresh()
      {
        this.this$0.refreshData();
      }
    };
    this.ListViewOnScrollListener = new AbsListView.OnScrollListener(this)
    {
      public void onScroll(, int paramInt1, int paramInt2, int paramInt3)
      {
      }

      public void onScrollStateChanged(, int paramInt)
      {
        switch (paramInt)
        {
        default:
        case 2:
        case 1:
        case 0:
        }
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return;
              this.this$0.mAdapter.setScrolling(true);
            }
            this.this$0.mAdapter.setScrolling(true);
          }
          this.this$0.mAdapter.scrollIdle(paramAbsListView);
        }
      }
    };
    this.mGetMoreListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "getMore";
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        super.onPostExecute(paramGenericTask, paramTaskResult);
        if (paramTaskResult != TaskResult.OK)
          Toast.makeText(this.this$0, this.this$0.mTask.getErrorMessage(), 0).show();
        this.this$0.mGetMoreButton.setText("更多");
        this.this$0.mAdapter.notifyDataSetChanged();
      }

      public void onProgressUpdate(, Object paramObject)
      {
        if (paramGenericTask instanceof CommonTask.QingboTask)
        {
          List localList = (List)paramObject;
          this.this$0.mAdapter.addQingboAtFoot(localList);
          QingboWithNoHeaderActivity.access$0(this.this$0, localList.size());
        }
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "QingboTask";
      }

      public void onCancelled()
      {
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (paramGenericTask instanceof QingboWithNoHeaderActivity.LocalDataTask)
        {
          this.this$0.mListView.onRefreshComplete();
          String str2 = IKnow.mSystemConfig.getString(this.this$0.TAG);
          if (!(StringUtil.isEmpty(str2)))
            this.this$0.mListView.setLastUpdated("更新于" + str2);
          this.this$0.mListView.prepareForRefresh();
          this.this$0.mListView.onRefresh();
          return;
        }
        if (paramTaskResult == TaskResult.OK)
        {
          String str1 = this.this$0.mDateFormat.format(new Date(System.currentTimeMillis()));
          IKnow.mSystemConfig.setString(this.this$0.TAG, str1);
          this.this$0.mListView.setLastUpdated("更新于" + str1);
        }
        while (true)
        {
          while (true)
          {
            this.this$0.mAdapter.notifyDataSetChanged();
            this.this$0.mListView.onRefreshComplete();
          }
          Toast.makeText(this.this$0, this.this$0.mTask.getErrorMessage(), 0).show();
        }
      }

      public void onProgressUpdate(, Object paramObject)
      {
        if (paramGenericTask instanceof QingboWithNoHeaderActivity.LocalDataTask)
        {
          List localList2 = (List)paramObject;
          if ((localList2 != null) && (localList2.size() > 0))
            this.this$0.mListViewFooter.setVisibility(0);
          this.this$0.mAdapter.addQingboAtHead(localList2);
        }
        while (true)
        {
          do
            return;
          while (!(paramGenericTask instanceof CommonTask.QingboTask));
          List localList1 = (List)paramObject;
          this.this$0.mAdapter.clearQingboList(0, localList1.size());
          this.this$0.mAdapter.addQingboAtHead(localList1);
          if ((localList1 != null) && (localList1.size() > 0))
            this.this$0.mListViewFooter.setVisibility(0);
          QingboWithNoHeaderActivity.access$0(this.this$0, localList1.size());
        }
      }
    };
  }

  private void initView()
  {
    if (getView() != 0)
      setContentView(getView());
    while (true)
    {
      this.mListView = ((PullToRefreshListView)findViewById(2131296321));
      this.mListView.setOnRefreshListener(this.ListViewRefreshListener);
      this.mListView.setOnItemClickListener(this.listItemClickListener);
      this.mListView.setOnScrollListener(this.ListViewOnScrollListener);
      setupListViewFooter();
      return;
      setContentView(2130903117);
    }
  }

  private void setupListViewFooter()
  {
    this.mListViewFooter = getLayoutInflater().inflate(2130903105, null);
    this.mGetMoreButton = ((Button)this.mListViewFooter.findViewById(2131296480));
    this.mGetMoreButton.setVisibility(0);
    this.mGetMoreButton.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.doNeedMore();
      }
    });
    this.mListView.addFooterView(this.mListViewFooter);
    this.mListView.setAdapter(this.mAdapter);
    this.mListViewFooter.setVisibility(8);
  }

  private void showNewQingboTips(int paramInt)
  {
    if (paramInt == 0)
      Toast.makeText(this, "没有新内容，先逛逛其他的吧", 0).show();
  }

  public void addView(int paramInt)
  {
    this.view = paramInt;
  }

  abstract List<QingBo> doGetLocolData();

  abstract void doNeedMore();

  protected int getCurrentListLength()
  {
    return this.mAdapter.getCount();
  }

  protected void getLocolData()
  {
    if ((this.mLocalDataTask != null) && (this.mLocalDataTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mListView.prepareForRefresh();
      this.mLocalDataTask = new LocalDataTask(this, null);
      this.mLocalDataTask.setListener(this.mTaskListener);
      this.mLocalDataTask.execute(new TaskParams[0]);
    }
  }

  abstract void getServerData();

  public int getView()
  {
    return this.view;
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    this.mAdapter = new QingBoAdapter(this, getLayoutInflater());
    initView();
  }

  protected void onDestroy()
  {
    if (this.mTask != null)
      this.mTask.cancel(true);
    this.mAdapter.clearQingBoList();
    super.onDestroy();
  }

  protected void onPause()
  {
    super.onPause();
    MobclickAgent.onPause(this);
  }

  protected void onResume()
  {
    super.onResume();
    MobclickAgent.onResume(this);
    MobclickAgent.setSessionContinueMillis(600000L);
  }

  public boolean onSearchRequested()
  {
    boolean bool;
    if (getParent() instanceof TabActivity)
      bool = getParent().onSearchRequested();
    while (true)
    {
      return bool;
      bool = super.onSearchRequested();
    }
  }

  public void onTabClick()
  {
    this.mListView.setSelection(1);
  }

  abstract void refreshData();

  private class LocalDataTask extends GenericTask
  {
    protected TaskResult _doInBackground()
    {
      List localList = this.this$0.doGetLocolData();
      if (localList != null)
        this.this$0.mLocalOffset = (this.this$0.mAdapter.getCount() + localList.size());
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = localList;
      publishProgress(arrayOfObject);
      return TaskResult.OK;
    }
  }
}